package com.meitianhui.convenitenbuy.module.wallet.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.wallet.presenter.WalletChangeRechargePresenter;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.StringUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class WalletChangeRechargeInputActivity extends BaseToolbarActivity  {
    private TextView mTvRemain;
    private EditText mEtRecharge;
    private Button mBtnConfirm;
    @Inject
    WalletChangeRechargePresenter mPresenter;


    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        initId();
        initInfo();
        setTbTitle(getString(R.string.wallet_change_recharge_title));
        isEnable(false);
        registerPush();
    }

    private void registerPush() {
        Disposable subscribe = RxBus.get().toFlowable(EventBean.class).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<EventBean>() {
            @Override
            public void accept(EventBean payResultBean) throws Exception {
                if (payResultBean != null) {
                    if (payResultBean.type == Constants.EVENT_TYPE_REFRESH) {
                        initInfo();
                    }
                }
            }
        });
        mCompositeDisposable.add(subscribe);
    }

    @Override
    protected int setView() {
        return R.layout.activity_wallet_change_recharge_input;
    }

    @Override
    protected void onRefreshData() {

    }

    /**
     * 初始化Id
     */
    private void initId() {
        mTvRemain = getViewById(R.id.tv_wallet_change_recharge_input_remain);
        mEtRecharge = getViewById(R.id.et_wallet_change_recharge_input);
        mBtnConfirm = getViewById(R.id.btn_wallet_change_recharge_input_confirm);
        RxUtils.bindEvents(mBtnConfirm, this);
    }

    /**
     * 初始化数据
     */
    private void initInfo() {
        mTvRemain.setText(InfoProvider.getUserAsset(App.getInstance()).getCash_balance());
    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.btn_wallet_change_recharge_input_confirm:
                checkInput();
                break;
        }
    }

    private void checkInput() {
        String input = mEtRecharge.getText().toString().trim();
        if (TextUtils.isEmpty(input)) {
            TipUtils.showToast(App.getInstance(), getString(R.string.wallet_change_recharge_hint), 3);
            return;
        } else if (!StringUtils.isAmount(input)) {
            TipUtils.showToast(App.getInstance(), getString(R.string.wallet_change_recharge_wrong), 3);
            mEtRecharge.setText("");
            return;
        } else if ("0".equals(input) || "0.".equals(input) || "0.0".equals(input) || "0.00".equals(input)) {
            TipUtils.showToast(App.getInstance(), getString(R.string.wallet_change_recharge_wrong), 3);
            mEtRecharge.setText("");
            return;
        }else if (StringUtils.moneyCompMore(input,"1000")){
            TipUtils.showToast(App.getInstance(), "每次充值金额不能超过¥1000！", 3);
            mEtRecharge.setText("");
            return;
        }

        Intent intent = new Intent(this, WalletChangeRechargeActivity.class);
        intent.putExtra(Constants.ARGUMENT, input);
        startActivity(intent);
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
